package com.demo.zhou.sniper.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import com.demo.zhou.sniper.R;
import com.demo.zhou.sniper.application.MyApplication;
import com.demo.zhou.sniper.dbmanger.SPManager;
import com.demo.zhou.sniper.gen.BiaoqianDao;
import com.demo.zhou.sniper.gen.ColorTableDao;
import com.demo.zhou.sniper.gen.DaoSession;
import com.demo.zhou.sniper.gen.QunbeanDao;
import com.demo.zhou.sniper.module.Biaoqian;
import com.demo.zhou.sniper.module.ColorTable;
import com.demo.zhou.sniper.module.D2Value;
import com.demo.zhou.sniper.module.MyValue;
import com.demo.zhou.sniper.module.Primitive;
import com.demo.zhou.sniper.module.Yxintiaojian;
import com.demo.zhou.sniper.module.Zubean;
import com.demo.zhou.sniper.util.tool.FangAnTool;
import com.demo.zhou.sniper.util.tool.Lookup;
import com.demo.zhou.sniper.util.tool.Yongyz;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Pattern;

import static android.content.ContentValues.TAG;

public class HomeActivity extends BaseActivity implements View.OnClickListener {

    public enum Colors {
        BLUE, RED
    }

    private Colors currentColor = Colors.BLUE;

    private RadioGroup rgMode, rgColor;
    private RadioButton rbL, rbB, rbGreen, rbBlue, rbRed;
    private Boolean isGreen = false;
    private Button tiaoPlus; // 条+
    private Button baochun; //保存
    private Button pay_keyboard_space, pay_keyboard_one, pay_keyboard_two,
            pay_keyboard_three, pay_keyboard_four, pay_keyboard_five,
            pay_keyboard_six, pay_keyboard_seven, pay_keyboard_eight,
            pay_keyboard_nine, pay_keyboard_zero, pay_keyboard_del,
            pay_keyboard_t, pay_keyboard_j, pay_keyboard_q, pay_keyboard_k;
    private TextView s_tv, currents_tv;
    private TextView xianshi_tv;
    private LinearLayout hsv_ll1;
    private LinearLayout hsv_ll2;
    private HorizontalScrollView hsv;//原始数显示
    private LinearLayout.LayoutParams lp1;
    List<Primitive> list;// 输入的原始数集合
    private int numS = 1;//总S
    private int currentS = 1;//当前s
    private static String numXianshi = "";
    private TextView tv_sum;
    private TextView tv_cuosum;
    int cuosum;
    List<Boolean>[] colorTableB;//色表B
    List<Boolean>[] colorTableL;//色表L
    List<Boolean>[] currentColorTable; //当前色表
    List<MyValue> my_vlist[][];
    List<D2Value> my_d2values[];
    List<D2Value> d2_sum;
    private Yxintiaojian yxintiaojian;
    List<Zubean> zubeans = new ArrayList<>();
    DaoSession daoSession;
    QunbeanDao qunbeanDao;
    ColorTableDao colorTableDao;
    Long qun_id;
    boolean lmode;
    private static int colort_num;
    List<ColorTable> colorTablels = new ArrayList<>();
    List<ColorTable> colorTablebs = new ArrayList<>();
    private TextView jiaquan_tv;
    private TextView total_tv;
    private TextView hen_tv;
    private int shaomiao_num = 0;

    class State {
        int jiaquan_dui;
        int jiaquan_total;
        int total_dui;
        int total_total;

        public State copy() {
            State s = new State();
            s.jiaquan_dui = jiaquan_dui;
            s.jiaquan_total = jiaquan_total;
            s.total_dui = total_dui;
            s.total_total = total_total;
            return s;
        }
    }

    List<State> states;
    private TextView biaoqian_tv;
    boolean lock;

    int cuoCounter[]; // for d2.5 if cuo >= 3 reverse results unless another cuo.
    boolean reversemode[];

    class MyHandler extends Handler {
        public int size = 0;
        public int counter = 0;

        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
            if (size != msg.arg1) {
                size = msg.arg1;
                counter = 0;
            }
            counter++;
            if (counter == zubeans.size()) {

                if (list.size() >= 4) {
                    boolean green = msg.arg2 == 1;
                    double value = 0;
                    double max_vaue = 0;
                    boolean mix_vaue = false;
                    //扫描
                    for (List<D2Value> d2 : my_d2values) {
                        if (d2.size() < 1) continue;
                        D2Value last = d2.get(d2.size() - 1);
                        if (Math.abs(last.getD2Value()) >= yxintiaojian.getTongcmix()) {
                            mix_vaue = true;
                        }
                        max_vaue += last.getD2Value();
                    }
                    if (mix_vaue && yxintiaojian.getYinyongz().equals("及时清洁")) {
                        int dui = 0;
                        int cuo = 0;
                        int tongj = 0;
                        for (List<D2Value> d2 : my_d2values) {
                            if (d2.size() < 2) continue;
                            if (d2.get(d2.size() - 2).isD2Duicuo()) {
                                dui++;
                            } else {
                                cuo++;
                                if (d2.get(d2.size() - 1).getD2Value() >= yxintiaojian.getTongcmix()) {
                                    tongj += d2.get(d2.size() - 1).getD2Value();
                                }
                            }
                        }
                        for (List<D2Value> d2 : my_d2values) {
                            if (d2.size() < 2) continue;
                            if (d2.get(d2.size() - 2).isD2Duicuo()) {
                                double d = d2.get(d2.size() - 1).getD2Value();
                                d2.get(d2.size() - 1).setD2Value((tongj * 0.7) / dui + d);
                                d2.get(d2.size() - 1).setD2ljValue(((tongj * 0.7) / dui + d) + d2.get(d2.size() - 2).getD2ljValue());
                            } else {
                                double d = d2.get(d2.size() - 1).getD2Value();
                                if (d >= yxintiaojian.getTongcmix()) {
                                    double volue = (tongj * 0.3) / cuo;
                                    d2.get(d2.size() - 1).setD2Value(volue);
                                    d2.get(d2.size() - 1).setD2ljValue(volue + d2.get(d2.size() - 2).getD2ljValue());
                                } else {
                                    double volue = (tongj * 0.3) / cuo;
                                    d2.get(d2.size() - 1).setD2Value(volue + d);
                                    d2.get(d2.size() - 1).setD2ljValue(volue + d + d2.get(d2.size() - 2).getD2ljValue());
                                }
                            }

                        }

                    }
                    for (List<D2Value> d2 : my_d2values) {
                        if (d2.size() < 1) continue;
                        D2Value last = d2.get(d2.size() - 1);
                        if (last.isD2mColor()) { // blue
                            value += last.getD2Value();
                        } else { // red negative
                            value -= last.getD2Value();
                        }
                    }

                    //综合
                    if (yxintiaojian.getTongcmax() < Math.abs(value) && yxintiaojian.getYinyongz().equals("及时清洁")) {
                        for (List<D2Value> d2 : my_d2values) {
                            if (d2.size() < 1) continue;
                            d2.get(d2.size() - 1).setD2Value(max_vaue / my_d2values.length);
                            if (d2.get(d2.size() - 1).isD2Duicuo()) {
                                d2.get(d2.size() - 1).setD2ljValue(max_vaue / my_d2values.length + d2.get(d2.size() - 2).getD2ljValue());
                            } else {
                                d2.get(d2.size() - 1).setD2ljValue(d2.get(d2.size() - 2).getD2ljValue() - max_vaue / my_d2values.length);
                            }
                        }
                        onShaoMiao(++shaomiao_num);
                    }
                    value = 0.0;
                    for (List<D2Value> d2 : my_d2values) {
                        if (d2.size() < 1) continue;
                        D2Value last = d2.get(d2.size() - 1);
                        if (last.isD2mColor()) { // blue
                            value += last.getD2Value();
                        } else { // red negative
                            value -= last.getD2Value();
                        }
                    }
                    D2Value newd2 = new D2Value();
                    newd2.setD2mColor(value >= 0);
                    double newvalue = Math.abs(value);
                    newd2.setD2Value(newvalue);
                    newd2.setD2ljValue(0);
                    newd2.setD2Lock(list.get(list.size() - 1).isAdengb() && !lmode);
                    if (d2_sum.size() > 0) {
                        D2Value oldd2 = d2_sum.get(d2_sum.size() - 1);
                        if (newd2.isD2Lock()) {
                            newd2.setD2ljValue(oldd2.getD2ljValue());
                        } else {
                            if (green) {
                                double cuoNum = 0.0;
                                for (List<D2Value> d2 : my_d2values) {
                                    if (d2.size() < 2) continue;
                                    D2Value prev = d2.get(d2.size() - 2);
//                            cuoNum += prev.getD2Value();
                                    if (prev.isD2mColor()) {
                                        cuoNum += prev.getD2Value();
                                    } else {
                                        cuoNum -= prev.getD2Value();
                                    }
                                }

                                if (cuoNum < 0) {
                                    newd2.setD2ljValue(oldd2.getD2ljValue() + cuoNum);
                                } else {
                                    newd2.setD2ljValue(oldd2.getD2ljValue() - cuoNum);
                                }

                            } else if (oldd2.isD2Duicuo()) {
                                newd2.setD2ljValue(oldd2.getD2ljValue() + oldd2.getD2Value());
                            } else {
                                newd2.setD2ljValue(oldd2.getD2ljValue() - oldd2.getD2Value());
                            }
                        }
                    }
                    d2_sum.add(newd2);
                } else {
                    TextView tv = new TextView(HomeActivity.this);
                    tv.setText("");
                    tv.setTextSize(18);
                    tv.setWidth(80);
                    hsv_ll2.addView(tv, lp1);
                }

                if (d2_sum != null && !d2_sum.isEmpty() && list.size() > 3) {
                    D2Value d2Value = d2_sum.get(d2_sum.size() - 1);
                    TextView tv = new TextView(HomeActivity.this);
                    TextPaint tp = tv.getPaint();
                    tp.setFakeBoldText(true);
                    tv.setTextSize(18);
                    if (d2Value.isD2mColor()) {
                        tv.setTextColor(HomeActivity.this.getResources().getColor(
                                R.color.blue));
                    } else {
                        tv.setTextColor(HomeActivity.this.getResources().getColor(
                                R.color.red));
                    }
                    long d2 = Math.round(d2Value.getD2Value());
//                    String format = String.format("%.2f",d2Value.getD2Value());
                    tv.setText("" + d2);
                    tv.setWidth(80);
                    hsv_ll2.addView(tv, lp1);
                    long ljz = Math.round(d2Value.getD2ljValue());
                    if (ljz < 0) {
                        tv_sum.setTextColor(HomeActivity.this.getResources().getColor(R.color.red));
                    } else {
                        tv_sum.setTextColor(HomeActivity.this.getResources().getColor(R.color.blue));
                    }
                    tv_sum.setText("" + Math.abs(ljz));

                    showState();
                }
                lock = false;
            }
        }
    }

    private void showState() {
        if (currentS > 5) {
            State state = states.get(states.size() - 1);
            if (state.jiaquan_total == 0) {
            } else {
                float jiaquan_per = state.jiaquan_dui * 100 / state.jiaquan_total;
                jiaquan_tv.setText(jiaquan_per / colort_num + "");
                float total_per = state.total_dui * 100 / state.total_total;
                total_tv.setText(total_per + "");
            }
            Double hen = 0.0;
            for (int i = 0; i < my_d2values.length; i++) {
                if (my_d2values[i].size() < 2) continue;
                if (my_d2values[i].get(my_d2values[i].size() - 2).isD2Duicuo()) {
                    hen++;
                }
            }
            hen_tv.setText(hen * 100 / my_d2values.length + "");

        } else {
            jiaquan_tv.setText(0 + "");
            total_tv.setText(0 + "");
            hen_tv.setText(0 + "");
        }
    }

    //    private Handler handler ;
    public MyHandler mainHandler = new MyHandler();

    private void reverseModeCheck(D2Value v, final int idx) {
        if (v.isD2Duicuo()) {
            cuoCounter[idx] = 0;
        } else {
            if (reversemode[idx]) {
                reversemode[idx] = false;
                cuoCounter[idx] = 0;
            } else {
                cuoCounter[idx]++;
                if (yxintiaojian.getDzhuangtai().equals("d2.5") && cuoCounter[idx] >= 3) {
                    reversemode[idx] = true;
                }
            }
        }
    }

    private String qunname;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("执行");
        setRightBtnVisible(true);
        Intent intent = getIntent();
        qun_id = Long.parseLong(intent.getStringExtra("qunid"));
        qunname = intent.getStringExtra("qunname");
        colort_num = intent.getIntExtra("colortnum", 1);
        initView();
        initClick();
        HandlerThread ht = new HandlerThread("Calculator Threads");
        ht.start();
        String primitives = intent.getStringExtra("primitives");
        if (primitives != null) {
            String mode = intent.getStringExtra("mode");
            if (mode.equals("L")) {
                rgMode.check(rbL.getId());
            } else {
                rgMode.check(rbB.getId());
            }
            process(primitives);
        }
    }

    private void process(final String primitives) {
        String prims[] = primitives.split(";");
        for (String p : prims) {
            String parts[] = p.split(",");
            String num = parts[0];
            Boolean color = parts[1].equals("1");
            Boolean green = parts[2].equals("1");
            char first = num.charAt(0);
            char second = num.charAt(1);
            int a = getCharInt(second);
            int b = getCharInt(first);
            int numInt = 0;
            if (a >= 10) {
                numInt = b * 100 + a;
            } else {
                numInt = b * 10 + a;
            }
            Primitive pri = new Primitive(numInt, color, green);
            if (green) {
                xianshi_tv.setTextColor(Color.GREEN);
            } else if (color) {
                xianshi_tv.setTextColor(Color.BLUE);
            } else {
                xianshi_tv.setTextColor(Color.RED);
            }
            xianshi_tv.setText(num);
            processPrimitiveSequentially(pri);
            prepareNewXianShi();
        }
    }

    @Override
    protected int getContentView() {
        return R.layout.activity_home;
    }

    private void setLModeLayout() {
        pay_keyboard_t.setEnabled(true);
        pay_keyboard_j.setEnabled(true);
        pay_keyboard_q.setEnabled(true);
        pay_keyboard_k.setEnabled(true);
        pay_keyboard_zero.setEnabled(false);
        currentColorTable = colorTableL;
        lmode = true;
        biaoqian_tv.setText(getLabel());
    }

    private void setBModeLayout() {
        pay_keyboard_t.setEnabled(false);
        pay_keyboard_j.setEnabled(false);
        pay_keyboard_q.setEnabled(false);
        pay_keyboard_k.setEnabled(false);
        pay_keyboard_zero.setEnabled(true);
        currentColorTable = colorTableB;
        lmode = false;
        biaoqian_tv.setText(getLabel());
    }

    public void disableRadioGroup(RadioGroup rg) {
        for (int i = 0; i < rg.getChildCount(); i++) {
            rg.getChildAt(i).setEnabled(false);
        }
    }

    public void enableRadioGroup(RadioGroup rg) {
        for (int i = 0; i < rg.getChildCount(); i++) {
            rg.getChildAt(i).setEnabled(true);
        }
    }

    private class RGModeOnChangeListener implements RadioGroup.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            RadioButton radioButton = (RadioButton) group.findViewById(checkedId);
            String mode = radioButton.getText().toString();
            if (mode.equals("L")) { // L mode
                setLModeLayout();
            } else { // B mode
                setBModeLayout();
            }
        }
    }

    private class RGColorOnChangeListener implements RadioGroup.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId) {
                case R.id.rbGreen:
                    xianshi_tv.setTextColor(HomeActivity.this.getResources().getColor(R.color.green));
                    isGreen = true;
                    int x = currentS % 8;
                    switch (x) {
                        case 1:
                        case 4:
                        case 6:
                        case 7:
                            currentColor = Colors.RED;
                            break;
                        default:
                            currentColor = Colors.BLUE;
                    }
                    break;
                case R.id.rbBlue:
                    xianshi_tv.setTextColor(HomeActivity.this.getResources().getColor(R.color.blue));
                    currentColor = Colors.BLUE;
                    isGreen = false;
                    break;
                case R.id.rbRed:
                    xianshi_tv.setTextColor(HomeActivity.this.getResources().getColor(R.color.red));
                    currentColor = Colors.RED;
                    isGreen = false;
                    break;
                default:
                    break;
            }
        }
    }

    @NonNull
    private TableRow.LayoutParams getRowLayoutParamsHeader(int row, int col) {
        TableRow.LayoutParams params = new TableRow.LayoutParams(
                100 * col, 80 * row);
        params.setMargins(4, 0, 4, 4);
        return params;
    }

    @NonNull
    private TableRow.LayoutParams getRowLayoutParams() {
        TableRow.LayoutParams params = new TableRow.LayoutParams(
                300, 80);
        params.setMargins(4, 0, 4, 4);
        return params;
    }

    @NonNull
    private TableLayout.LayoutParams getTblLayoutParams() {
        return new TableLayout.LayoutParams(
                LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT);
    }

    private TextView getTextView(int id, String title, int color, int typeface, int bgColor, TableRow.LayoutParams params) {
        TextView tv = new TextView(this);
        tv.setId(id);
        tv.setText(title.toUpperCase());
        tv.setTextColor(color);
        tv.setTextSize(16);
//        tv.setPadding(40, 40, 40, 40);
        tv.setTypeface(Typeface.DEFAULT, typeface);
        tv.setBackgroundColor(bgColor);
        tv.setLayoutParams(params);
        tv.setGravity(Gravity.CENTER);

        return tv;
    }

    private class ShuziOnclickListener implements View.OnClickListener {

        private View myPop;

        public ShuziOnclickListener(View pop) {
            myPop = pop;
        }

        @Override
        public void onClick(View v) {
            if (v instanceof TextView) {
                TextView tv = (TextView) v;
                String text = tv.getText().toString();
                if (text.equals("数字")) {
                    tv.setText("对错");
                    addData(myPop, false);
                } else {
                    tv.setText("数字");
                    addData(myPop, true);
                }
            }
        }
    }

    private void addHeaders(View popupView) {
        TableLayout tl_row = (TableLayout) popupView.findViewById(R.id.table_header_row);
        TableRow tr = new TableRow(popupView.getContext());
        TableRow.LayoutParams params = getRowLayoutParamsHeader(2, 2);
        params.column = 0;
        params.span = 2;
        tr.setLayoutParams(params);
        TextView shuziview = getTextView(0, "数字", Color.BLUE, Typeface.BOLD, Color.WHITE, params);
        shuziview.setOnClickListener(new ShuziOnclickListener(popupView));
        tr.addView(shuziview);
        tl_row.addView(tr, getTblLayoutParams());

        for (int i = list.size() - 1; i >= 4; --i) {
            int idx = i - 4;
            Primitive prim = list.get(i);
            tr = new TableRow(popupView.getContext());
            tr.setLayoutParams(getRowLayoutParamsHeader(1, 1));
            tr.addView(getTextView(0, (idx + 1) + "", Color.BLACK, Typeface.NORMAL, Color.WHITE, getRowLayoutParamsHeader(1, 1)));
            int color = prim.isColor() ? Color.BLUE : Color.RED;
            tr.addView(getTextView(0, prim.toString(), color, Typeface.NORMAL, Color.WHITE, getRowLayoutParamsHeader(1, 1)));
            tl_row.addView(tr, getTblLayoutParams());
        }

        TableLayout tl2_col = (TableLayout) popupView.findViewById(R.id.table_header_col);
        TableRow tr2 = new TableRow(popupView.getContext());
        tr2.setLayoutParams(getRowLayoutParams());
        for (Zubean zb : zubeans) {
            String txt;
            if (zb.getMos()) {
                txt = zb.getXian() + "s" + zb.getFanganhao();
            } else {
                txt = zb.getXian() + "y" + zb.getFanganhao();
            }
            for (int k = 1; k <= colort_num; k++) {
                if (k != colort_num) {
                    tr2.addView(getTextView(0, txt + "_" + k, Color.BLACK, Typeface.BOLD, Color.WHITE, getRowLayoutParams()));
                } else {
                    tr2.addView(getTextView(0, txt + "_o", Color.BLACK, Typeface.BOLD, Color.WHITE, getRowLayoutParams()));
                }
            }

        }
        tr2.addView(getTextView(0, "合", Color.BLACK, Typeface.BOLD, Color.WHITE, getRowLayoutParams()));
        tr2.addView(getTextView(0, "累", Color.BLACK, Typeface.BOLD, Color.WHITE, getRowLayoutParams()));
        tl2_col.addView(tr2, getTblLayoutParams());
        tr2 = new TableRow(popupView.getContext());
        tr2.setLayoutParams(getRowLayoutParams());
        for (List<D2Value> d2vs : my_d2values) {
            int i = -1;
            int locks = 0;
            for (D2Value d2 : d2vs) {
                if (!d2.isD2Lock()) {
                    if (d2.isD2Duicuo()) {
                        ++i;
                    }
                } else {
                    ++locks;
                }
            }
            int duirate = d2vs.size() <= 2 ? 0 : i * 100 / (d2vs.size() - 2 - locks);
            tr2.addView(getTextView(0, duirate + "", Color.BLACK, Typeface.BOLD, Color.WHITE, getRowLayoutParams()));
        }
        int i_sum = 0;
        int locks_sum = 0;
        for (D2Value d2 : d2_sum) {
            if (!d2.isD2Lock()) {
                if (d2.isD2Duicuo()) {
                    ++i_sum;
                }
            } else {
                ++locks_sum;

            }
        }
        int duirate_sum = d2_sum.size() <= 2 ? 0 : i_sum * 100 / (d2_sum.size() - 1 - locks_sum);
        tr2.addView(getTextView(0, duirate_sum + "", Color.BLACK, Typeface.BOLD, Color.WHITE, getRowLayoutParams()));
        tr2.addView(getTextView(0, "计", Color.BLACK, Typeface.BOLD, Color.WHITE, getRowLayoutParams()));
        tl2_col.addView(tr2, getTblLayoutParams());
    }

    private void addData(View popupView, boolean isDuicuo) {
        TableLayout tl2 = popupView.findViewById(R.id.table_duicuo);
        tl2.removeAllViews();

        for (int i = list.size() - 1; i >= 4; --i) {
            int idx = i - 4;

            TableRow tr2 = new TableRow(popupView.getContext());
            tr2.setLayoutParams(getRowLayoutParams());
            for (List<D2Value> d2vs : my_d2values) {
                D2Value d2 = d2vs.get(idx + 1);
                if (isDuicuo) {
                    if (d2.isD2Duicuo()) {
                        tr2.addView(getTextView(0, "\u2714", Color.BLUE, Typeface.BOLD, Color.WHITE, getRowLayoutParams()));
                    } else {
                        tr2.addView(getTextView(0, "\u2715", Color.RED, Typeface.BOLD, Color.WHITE, getRowLayoutParams()));
                    }
                } else {
                    if (d2.isD2mColor()) {
                        tr2.addView(getTextView(0, Math.round(d2.getD2Value()) + "", Color.BLUE, Typeface.BOLD, Color.WHITE, getRowLayoutParams()));
                    } else {
                        tr2.addView(getTextView(0, Math.round(d2.getD2Value()) + "", Color.RED, Typeface.BOLD, Color.WHITE, getRowLayoutParams()));
                    }
                }
            }
            D2Value d2sum = d2_sum.get(idx);
            if (d2sum.isD2Duicuo()) {
                tr2.addView(getTextView(0, Math.round(d2sum.getD2Value()) + "", Color.BLUE, Typeface.BOLD, Color.WHITE, getRowLayoutParams()));
            } else {
                tr2.addView(getTextView(0, Math.round(d2sum.getD2Value()) + "", Color.RED, Typeface.BOLD, Color.WHITE, getRowLayoutParams()));
            }
            long ljz = Math.round(d2sum.getD2ljValue());
            if (ljz >= 0) {
                tr2.addView(getTextView(0, Math.abs(ljz) + "", Color.BLUE, Typeface.BOLD, Color.WHITE, getRowLayoutParams()));
            } else {
                tr2.addView(getTextView(0, Math.abs(ljz) + "", Color.RED, Typeface.BOLD, Color.WHITE, getRowLayoutParams()));
            }
            tl2.addView(tr2, getTblLayoutParams());
        }
    }

    private PopupWindow prepareDuiCuoWindow() {
        View popupView = getLayoutInflater().inflate(R.layout.duicuo_popup, null);
        addHeaders(popupView);
        addData(popupView, true);
        PopupWindow mDuicuoWindow = new PopupWindow(popupView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, true);
        mDuicuoWindow.setTouchable(true);
        mDuicuoWindow.setOutsideTouchable(true);
//        mDuicuoWindow.setBackgroundDrawable(new BitmapDrawable(getResources(), (Bitmap) null));
        return mDuicuoWindow;
    }

    // 初始化控件
    private void initView() {

        Button showDuiCuo = findViewById(R.id.show_duicuo);
        showDuiCuo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (SPManager.getInstance().getInt("Jurisdiction", 5) < 2) {
                    PopupWindow duicuo = prepareDuiCuoWindow();
                    duicuo.showAtLocation(v, Gravity.LEFT | Gravity.TOP, 0, 0);
                }
            }
        });
        rgMode = findViewById(R.id.rgMode);
        rgMode.setOnCheckedChangeListener(new RGModeOnChangeListener());
        disableRadioGroup(rgMode);
        rgColor = findViewById(R.id.rgColor);
        rgColor.setOnCheckedChangeListener(new RGColorOnChangeListener());
        rbGreen = findViewById(R.id.rbGreen);
        rbGreen.setEnabled(false);
        rbL = findViewById(R.id.rbL);
        rbB = findViewById(R.id.rbB);
        rbBlue = findViewById(R.id.rbBlue);
        rbRed = findViewById(R.id.rbRed);

        tiaoPlus = findViewById(R.id.tiaoPlus);
        baochun = findViewById(R.id.baochun);
        hsv = findViewById(R.id.hsv);
        s_tv = findViewById(R.id.s_tv);
        currents_tv = findViewById(R.id.currents_tv);
        hsv_ll1 = findViewById(R.id.horizon_ll1);
        hsv_ll2 = findViewById(R.id.horizon_ll2);
        pay_keyboard_space = findViewById(
                R.id.b_keyboard_space);
        pay_keyboard_one = findViewById(
                R.id.b_keyboard_one);
        pay_keyboard_two = findViewById(
                R.id.b_keyboard_two);
        pay_keyboard_three = findViewById(
                R.id.b_keyboard_three);
        pay_keyboard_four = findViewById(
                R.id.b_keyboard_four);
        pay_keyboard_five = findViewById(
                R.id.b_keyboard_five);
        pay_keyboard_six = findViewById(
                R.id.b_keyboard_six);
        pay_keyboard_seven = findViewById(
                R.id.b_keyboard_seven);
        pay_keyboard_eight = findViewById(
                R.id.b_keyboard_eight);
        pay_keyboard_nine = findViewById(
                R.id.b_keyboard_nine);
        pay_keyboard_zero = findViewById(
                R.id.b_keyboard_zero);
        pay_keyboard_del = findViewById(
                R.id.b_keyboard_del);
        pay_keyboard_t = findViewById(
                R.id.b_keyboard_t);
        pay_keyboard_j = findViewById(
                R.id.b_keyboard_j);
        pay_keyboard_q = findViewById(
                R.id.b_keyboard_q);
        pay_keyboard_k = findViewById(
                R.id.b_keyboard_k);
        tv_sum = findViewById(R.id.tv_sum);
        jiaquan_tv = findViewById(R.id.jiaquan_percent);
        total_tv = findViewById(R.id.total_percent);
        hen_tv = findViewById(R.id.hen_percent);
        biaoqian_tv = findViewById(R.id.biaoqian);
        biaoqian_tv.setText(getLabel());
        biaoqian_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                biaoqianSave();
            }
        });
        TextView tv_xianshu = findViewById(R.id.xianshu);
        TextView tv_qun = findViewById(R.id.qun);
        TextView tv_jishu = findViewById(R.id.jishu);
        TextView tv_suanfa = findViewById(R.id.suanfa);
        tv_cuosum = findViewById(R.id.cuosum);
        lp1 = new LinearLayout.LayoutParams(230,
                LayoutParams.WRAP_CONTENT);
        list = new ArrayList<Primitive>();
        lp1.setMargins(10, 0, 10, 0);
        xianshi_tv = new TextView(HomeActivity.this);
        TextPaint tp = xianshi_tv.getPaint();
        tp.setFakeBoldText(true);
        xianshi_tv.setTextSize(18);
        xianshi_tv.setText("");
        hsv_ll1.addView(xianshi_tv, lp1);
        TextView tv = new TextView(HomeActivity.this);
        tv.setText("");
        tv.setTextSize(18);
        hsv_ll2.addView(tv, lp1);
        yxintiaojian = getYxintiaojian();
        tv_xianshu.setText(yxintiaojian.getGongzxian() + "");
        tv_qun.setText(qunname + "");
        tv_jishu.setText(yxintiaojian.getJshu() + "");
        tv_suanfa.setText(yxintiaojian.getYinyongz() + "");
        daoSession = ((MyApplication) getApplication()).getDaoSession();
        if (yxintiaojian.getGongzxian() > 0) {
            zubeans = getZubeans(qun_id, yxintiaojian.getGongzxian());
        }
        colorTableDao = daoSession.getColorTableDao();
        List<ColorTable> colorTables = colorTableDao.loadAll();
        for (ColorTable colorTable : colorTables) {
            if (colorTable.getCt_type().equals("B")) {
                colorTablebs.add(colorTable);
            } else if (colorTable.getCt_type().equals("L")) {
                colorTablels.add(colorTable);
            }
        }
        if (colort_num > colorTablebs.size() || colort_num > colorTablels.size()) {
            colort_num = colorTablebs.size() > colorTablels.size() ? colorTablels.size() : colorTablebs.size();
        }
        my_vlist = new ArrayList[zubeans.size() * colort_num][];
        for (int i = 0; i < zubeans.size(); ++i) {
            for (int k = 0; k < colort_num; k++) {
                my_vlist[i * colort_num + k] = new ArrayList[zubeans.get(i).getXian()];
                for (int j = 0; j < zubeans.get(i).getXian(); ++j) {
                    my_vlist[i * colort_num + k][j] = new ArrayList<>();
                }
            }
        }
        my_d2values = new ArrayList[yxintiaojian.getGongzxian() * colort_num];
        for (int i = 0; i < my_d2values.length; i++) {
            my_d2values[i] = new ArrayList<>();
        }
        d2_sum = new ArrayList<>();
        states = new ArrayList<>();
        cuoCounter = new int[yxintiaojian.getGongzxian() * colort_num];
        reversemode = new boolean[yxintiaojian.getGongzxian() * colort_num];

        // set b 色表
        colorTableB = new List[colort_num];
        for (int i = 0; i < colort_num; i++) {
            char[] chb = colorTablebs.get(i).getCt_centon().toCharArray();
            colorTableB[i] = new ArrayList<>();
            for (char aCh : chb) {
                if (String.valueOf(aCh).equals("d")) {
                    colorTableB[i].add(true);
                } else {
                    colorTableB[i].add(false);
                }
            }
        }
        // set l 色表
        colorTableL = new List[colort_num];
        for (int i = 0; i < colort_num; i++) {
            char[] chb = colorTablels.get(i).getCt_centon().toCharArray();
            colorTableL[i] = new ArrayList<>();
            for (char aCh : chb) {
                if (String.valueOf(aCh).equals("d")) {
                    colorTableL[i].add(true);
                } else {
                    colorTableL[i].add(false);
                }
            }
        }

        setBModeLayout();
    }

    public void initClick() {
        tiaoPlus.setOnClickListener(this);
        baochun.setOnClickListener(this);
        pay_keyboard_space.setOnClickListener(this);
        pay_keyboard_one.setOnClickListener(this);
        pay_keyboard_two.setOnClickListener(this);
        pay_keyboard_three.setOnClickListener(this);
        pay_keyboard_four.setOnClickListener(this);
        pay_keyboard_five.setOnClickListener(this);
        pay_keyboard_six.setOnClickListener(this);
        pay_keyboard_seven.setOnClickListener(this);
        pay_keyboard_eight.setOnClickListener(this);
        pay_keyboard_nine.setOnClickListener(this);
        pay_keyboard_zero.setOnClickListener(this);
        pay_keyboard_del.setOnClickListener(this);
        pay_keyboard_t.setOnClickListener(this);
        pay_keyboard_j.setOnClickListener(this);
        pay_keyboard_q.setOnClickListener(this);
        pay_keyboard_k.setOnClickListener(this);
    }

    private void biaoqianSave() {
        final EditText et = new EditText(this);

        new AlertDialog.Builder(this).setTitle("标签修改")
                .setIcon(android.R.drawable.ic_dialog_info)
                .setView(et)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        String input = et.getText().toString();
                        if (input.equals("")) {
                            Toast.makeText(getApplicationContext(), "" +
                                    "标签编号不能为空！", Toast.LENGTH_LONG).show();
                        } else {
                            biaoqian_tv.setText(input);
                        }
                    }
                })
                .setNegativeButton("取消", null)
                .show();
    }

    private int getNumCores() {
        //Private Class to display only CPU devices in the directory listing
        class CpuFilter implements FileFilter {
            @Override
            public boolean accept(File pathname) {
                //Check if filename is "cpu", followed by a single digit number
                if (Pattern.matches("cpu[0-9]", pathname.getName())) {
                    return true;
                }
                return false;
            }
        }

        try {
            //Get directory containing CPU info
            File dir = new File("/sys/devices/system/cpu/");
            //Filter to only list the devices we care about
            File[] files = dir.listFiles(new CpuFilter());
            Log.d(TAG, "CPU Count: " + files.length);
            //Return the number of cores (virtual CPU devices)
//            return files.length;
            return 1;
        } catch (Exception e) {
            //Print exception
            Log.d(TAG, "CPU Count: Failed.");
            e.printStackTrace();
            //Default to return 1 core
            return 1;
        }
    }

    private void handleTiaoPlus() {
        AlertDialog.Builder builder = new AlertDialog.Builder(HomeActivity.this);
        builder.setMessage("确定条+?");
        builder.setTitle("提示");

        //添加AlertDialog.Builder对象的setPositiveButton()方法
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                enableRadioGroup(rgMode);
//                currentS = 1;
//                currents_tv.setText("" + currentS);
                rollbackTiaoJia();
                // TODO
                biaoqian_tv.setText(getLabel());
                Toast.makeText(HomeActivity.this, "条+！", Toast.LENGTH_SHORT).show();
            }
        });

        //添加AlertDialog.Builder对象的setNegativeButton()方法
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.create().show();
    }

    private void handleSave() {
        AlertDialog.Builder builder = new AlertDialog.Builder(HomeActivity.this);
        builder.setMessage("确定保存?");
        builder.setTitle("提示");

        //添加AlertDialog.Builder对象的setPositiveButton()方法
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String label = biaoqian_tv.getText().toString();
                List<String> primitives = new ArrayList<>();
                for (Primitive pri : list) {
                    String primitive = pri.toString();
                    String color = pri.isColor() ? "1" : "0";
                    String green = pri.isGreen() ? "1" : "0";
                    primitives.add(pri.toString() + "," + color + "," + green);
                }

                BiaoqianDao bd = daoSession.getBiaoqianDao();
                Biaoqian bq = new Biaoqian();
                bq.setLabel(label);
                bq.setPrimitives(TextUtils.join(";", primitives));
                bd.insert(bq);
                biaoqian_tv.setText(getLabel());
                rollbackBaoCun();
                Toast.makeText(HomeActivity.this, "保存！", Toast.LENGTH_SHORT).show();
            }
        });

        //添加AlertDialog.Builder对象的setNegativeButton()方法
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.create().show();
    }

    private ExecutorService service = Executors.newFixedThreadPool(getNumCores());

    private void resetColorRadio() {
        switch (currentColor) {
            case BLUE:
                rgColor.check(rbBlue.getId());
                break;
            case RED:
                rgColor.check(rbRed.getId());
                break;
        }
    }

    private void setTextColor(Boolean eq) {
        if (eq) {
            if (lmode) {
                rbGreen.setEnabled(true);
            } else {
                int x = currentS % 8;
                switch (x) {
                    case 1:
                    case 4:
                    case 6:
                    case 7:
                        rgColor.check(R.id.rbRed);
                        break;
                    default:
                        rgColor.check(R.id.rbBlue);
                }
                rbBlue.setEnabled(false);
                rbRed.setEnabled(false);
            }
        } else {
            rbGreen.setEnabled(false);
            rbBlue.setEnabled(true);
            rbRed.setEnabled(true);
        }
        switch (currentColor) {
            case BLUE:
                xianshi_tv.setTextColor(this.getResources().getColor(R.color.blue));
                break;
            case RED:
                xianshi_tv.setTextColor(this.getResources().getColor(R.color.red));
                break;
        }
    }

    private int getCharInt(char c) {
        int res = 0;
        switch (c) {
            case '0':
                res = 0;
                break;
            case '1':
                res = 1;
                break;
            case '2':
                res = 2;
                break;
            case '3':
                res = 3;
                break;
            case '4':
                res = 4;
                break;
            case '5':
                res = 5;
                break;
            case '6':
                res = 6;
                break;
            case '7':
                res = 7;
                break;
            case '8':
                res = 8;
                break;
            case '9':
                res = 9;
                break;
            case 'T':
                res = 10;
                break;
            case 'J':
                res = 11;
                break;
            case 'Q':
                res = 12;
                break;
            case 'K':
                res = 13;
                break;
        }
        return res;
    }

    private void prepareNewXianShi() {
        numXianshi = "";
        xianshi_tv = new TextView(HomeActivity.this);
        TextPaint tp = xianshi_tv.getPaint();
        tp.setFakeBoldText(true);
        xianshi_tv.setTextSize(18);
        xianshi_tv.setText(numXianshi);
        xianshi_tv.setWidth(80);
        hsv_ll1.addView(xianshi_tv, lp1);
        hsv.postDelayed(new Runnable() {
            @Override
            public void run() {
                hsv.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
            }
        }, 50);
        rbBlue.setEnabled(true);
        rbRed.setEnabled(true);
        rbGreen.setEnabled(false);
        resetColorRadio();
    }

    private void processPrimitiveSequentially(Primitive pri) {
        list.add(pri);
        State state = states.isEmpty() ? new State() : states.get(states.size() - 1).copy();
        states.add(state);
        numS++;
        s_tv.setText("总S: " + numS);
        currentS++;
        currents_tv.setText("" + currentS);
        for (int i = 0; i < zubeans.size(); ++i) {
            Zubean zb = zubeans.get(i);
            for (int k = 0; k < colort_num; k++) {
                getD2Values(new Lookup(currentColorTable[k]), list, zb, my_vlist[i * colort_num + k]);
            }
        }
        if (yxintiaojian.isZtaixz()) {
            getZhuD2(pri);

        } else {
            getXianD2(pri);
        }
        if (d2_sum.size() > 0 && list.size() > 4) {
            D2Value d2Value = d2_sum.get(d2_sum.size() - 1);
            if (list.get(list.size() - 1).isColor()) {
                if (d2Value.isD2mColor()) {
                    d2Value.setD2Duicuo(true);
                } else {
                    d2Value.setD2Duicuo(false);
                }
            } else {
                if (d2Value.isD2mColor()) {
                    d2Value.setD2Duicuo(false);
                } else {
                    d2Value.setD2Duicuo(true);
                }
            }
            if (!d2Value.isD2Lock()) {
                State s = states.get(states.size() - 1);
                if (d2Value.isD2Duicuo()) {
                    s.total_dui++;
                }
                s.jiaquan_total += yxintiaojian.getGongzxian();
                s.total_total++;
                if (!d2Value.isD2Duicuo() || pri.isGreen()) {
                    cuosum += Math.abs(Math.round(d2Value.getD2Value()));
                    tv_cuosum.setText(cuosum + "");
                }
                states.add(s);
            }
        }

        if (list.size() >= 4) {
            boolean green = pri.isGreen();
            double value = 0;
            double max_vaue = 0;
            boolean mix_vaue = false;
            int jurisdiction = SPManager.getInstance().getInt("Jurisdiction", 5);
            //扫描
            for (List<D2Value> d2 : my_d2values) {
                if (d2.size() < 1) continue;
                D2Value last = d2.get(d2.size() - 1);
                if (Math.abs(last.getD2Value()) >= yxintiaojian.getTongcmix()) {
                    mix_vaue = true;
                }
                max_vaue += last.getD2Value();
            }
            if (mix_vaue && yxintiaojian.getYinyongz().equals("及时清洁") && jurisdiction < 4) {
                int dui = 0;
                int cuo = 0;
                int tongj = 0;
                for (List<D2Value> d2 : my_d2values) {
                    if (d2.size() < 2) continue;
                    if (d2.get(d2.size() - 2).isD2Duicuo()) {
                        dui++;
                    } else {
                        cuo++;
                        if (d2.get(d2.size() - 1).getD2Value() >= yxintiaojian.getTongcmix()) {
                            tongj += d2.get(d2.size() - 1).getD2Value();
                        }
                    }
                }
                for (List<D2Value> d2 : my_d2values) {
                    if (d2.size() < 2) continue;
                    if (d2.get(d2.size() - 2).isD2Duicuo()) {
                        double d = d2.get(d2.size() - 1).getD2Value();
                        d2.get(d2.size() - 1).setD2Value((tongj * 0.7) / dui + d);
                        d2.get(d2.size() - 1).setD2ljValue(((tongj * 0.7) / dui + d) + d2.get(d2.size() - 2).getD2ljValue());
                    } else {
                        if (d2.size() < 1) continue;
                        double d = d2.get(d2.size() - 1).getD2Value();
                        if (d >= yxintiaojian.getTongcmix()) {
                            double volue = (tongj * 0.3) / cuo;
                            d2.get(d2.size() - 1).setD2Value(volue);
                            d2.get(d2.size() - 1).setD2ljValue(volue + d2.get(d2.size() - 2).getD2ljValue());
                        } else {
                            double volue = (tongj * 0.3) / cuo;
                            d2.get(d2.size() - 1).setD2Value(volue + d);
                            d2.get(d2.size() - 1).setD2ljValue(volue + d + d2.get(d2.size() - 2).getD2ljValue());
                        }
                    }

                }

            }
            for (List<D2Value> d2 : my_d2values) {
                if (d2.size() < 1) continue;
                D2Value last = d2.get(d2.size() - 1);
                if (last.isD2mColor()) { // blue
                    value += last.getD2Value();
                } else { // red negative
                    value -= last.getD2Value();
                }
            }

            //综合
            if (yxintiaojian.getTongcmax() < Math.abs(value) && yxintiaojian.getYinyongz().equals("及时清洁") && jurisdiction < 4) {
                for (List<D2Value> d2 : my_d2values) {
                    if (d2.size() < 1) continue;
                    d2.get(d2.size() - 1).setD2Value(max_vaue / my_d2values.length);
                    if (d2.get(d2.size() - 1).isD2Duicuo()) {
                        d2.get(d2.size() - 1).setD2ljValue(max_vaue / my_d2values.length + d2.get(d2.size() - 2).getD2ljValue());
                    } else {
                        d2.get(d2.size() - 1).setD2ljValue(d2.get(d2.size() - 2).getD2ljValue() - max_vaue / my_d2values.length);
                    }
                }
                onShaoMiao(++shaomiao_num);
            }
            value = 0.0;
            for (List<D2Value> d2 : my_d2values) {
                if (d2.size() < 1) continue;
                D2Value last = d2.get(d2.size() - 1);
                if (last.isD2mColor()) { // blue
                    value += last.getD2Value();
                } else { // red negative
                    value -= last.getD2Value();
                }
            }
            D2Value newd2 = new D2Value();
            newd2.setD2mColor(value >= 0);
            double newvalue = Math.abs(value);
            newd2.setD2Value(newvalue);
            newd2.setD2ljValue(0);
            newd2.setD2Lock(list.get(list.size() - 1).isAdengb() && !lmode);
            if (d2_sum.size() > 0) {
                D2Value oldd2 = d2_sum.get(d2_sum.size() - 1);
                if (newd2.isD2Lock()) {
                    newd2.setD2ljValue(oldd2.getD2ljValue());
                } else {
                    if (green) {
                        double cuoNum = 0.0;
                        for (List<D2Value> d2 : my_d2values) {
                            if (d2.size() < 2) continue;
                            D2Value prev = d2.get(d2.size() - 2);
//                            cuoNum += prev.getD2Value();
                            if (prev.isD2mColor()) {
                                cuoNum += prev.getD2Value();
                            } else {
                                cuoNum -= prev.getD2Value();
                            }
                        }

                        if (cuoNum < 0) {
                            newd2.setD2ljValue(oldd2.getD2ljValue() + cuoNum);
                        } else {
                            newd2.setD2ljValue(oldd2.getD2ljValue() - cuoNum);
                        }

                    } else if (oldd2.isD2Duicuo()) {
                        newd2.setD2ljValue(oldd2.getD2ljValue() + oldd2.getD2Value());
                    } else {
                        newd2.setD2ljValue(oldd2.getD2ljValue() - oldd2.getD2Value());
                    }
                }
            }
            d2_sum.add(newd2);
        } else {
            TextView tv = new TextView(HomeActivity.this);
            tv.setText("");
            tv.setTextSize(18);
            tv.setWidth(80);
            hsv_ll2.addView(tv, lp1);
        }

        if (d2_sum != null && !d2_sum.isEmpty() && list.size() > 3) {
            D2Value d2Value = d2_sum.get(d2_sum.size() - 1);
            TextView tv = new TextView(HomeActivity.this);
            TextPaint tp = tv.getPaint();
            tp.setFakeBoldText(true);
            tv.setTextSize(18);
            if (d2Value.isD2mColor()) {
                tv.setTextColor(HomeActivity.this.getResources().getColor(
                        R.color.blue));
            } else {
                tv.setTextColor(HomeActivity.this.getResources().getColor(
                        R.color.red));
            }
            long d2 = Math.round(d2Value.getD2Value());
//                    String format = String.format("%.2f",d2Value.getD2Value());
            tv.setText("" + d2);
            tv.setWidth(80);
            hsv_ll2.addView(tv, lp1);
            long ljz = Math.round(d2Value.getD2ljValue());
            if (ljz < 0) {
                tv_sum.setTextColor(HomeActivity.this.getResources().getColor(R.color.red));
            } else {
                tv_sum.setTextColor(HomeActivity.this.getResources().getColor(R.color.blue));
            }
            tv_sum.setText("" + Math.abs(ljz));

            showState();
        }

    }

    private void getXianD2(Primitive pri) {
        List<Object> mvs_list = new ArrayList<>();
        int h = 0;
        for (int i = 0; i < zubeans.size(); i++) {
            h += zubeans.get(i).getXian();
        }
        int l = h - yxintiaojian.getGongzxian();
        if (l == 0) {
            for (int i = 0; i < my_vlist.length; i++) {
                for (int j = 0; j < my_vlist[i].length; j++) {
                    mvs_list.add(my_vlist[i][j]);
                }
            }
        } else {
            //my_vlist的长度是组的长度乘上色表数
            for (int i = 0; i < zubeans.size() - 1; i++) {
                for (int j = 0; j < colort_num; j++) {
                    for (int k = 0; k < my_vlist[j].length; k++) {
                        mvs_list.add(my_vlist[i * j][k]);
                    }
                }
            }
            for (int i = 0; i < colort_num; i++) {
                for (int j = 0; j < zubeans.get(zubeans.size() - 1).getXian() - l; j++) {
                    mvs_list.add(my_vlist[(zubeans.size() - 1) * colort_num + i][j]);
                }
            }

        }
        List<MyValue> mvsd1[] = new ArrayList[yxintiaojian.getGongzxian() * colort_num];
        for (int i = 0; i < mvs_list.size(); i++) {
            mvsd1[i] = (List<MyValue>) mvs_list.get(i);
        }
        for (int i = 0; i < mvs_list.size(); i++) {
            List<D2Value> d2Values = my_d2values[i];
            List<MyValue> mvs = mvsd1[i];
            if (list.size() == 4) {
                if (d2Values.size() < 1) {
                    D2Value d2Value = new D2Value();
                    d2Value.setD2mColor(true);
                    d2Values.add(d2Value);
                }

            }
            if (list.size() > 3) {
                switch (yxintiaojian.getYinyongz()) {
                    case "及时清洁":
                        Yongyz.qingQi(getXD2list(d2Values, mvs, list, i, pri.isGreen()), yxintiaojian);
                        break;
                    case "斐波":
                        Yongyz.feiBo(getXD2list(d2Values, mvs, list, i, pri.isGreen()), yxintiaojian);
                        break;
                    case "越级":
                        Yongyz.yuJi(getXD2list(d2Values, mvs, list, i, pri.isGreen()), yxintiaojian);
                        break;
                    case "横向124":
                        Yongyz.hengXiang124(getXD2list(d2Values, mvs, list, i, pri.isGreen()), yxintiaojian);
                    case "横向137":
                        Yongyz.hengXiang137(getXD2list(d2Values, mvs, list, i, pri.isGreen()), yxintiaojian);
                        break;
                    default:
                        break;
                }
                my_d2values[i] = d2Values;
            }

        }
    }

    private void getZhuD2(Primitive pri) {
        for (int i = 0; i < zubeans.size(); ++i) {
            for (int k = 0; k < colort_num; k++) {
                List<D2Value> d2Values = my_d2values[i * colort_num + k];
                List<MyValue> mvs[] = my_vlist[i * colort_num + k];
                if (list.size() == 4) {
                    if (d2Values.size() < 1) {
                        D2Value d2Value = new D2Value();
                        d2Value.setD2mColor(true);
                        d2Values.add(d2Value);
                    } else {
                        d2Values.get(d2Values.size() - 1).setD2mColor(true);

                    }

                }
                if (list.size() > 3) {
                    switch (yxintiaojian.getYinyongz()) {
                        case "及时清洁":
                            Yongyz.qingQi(getD2list(d2Values, mvs, list, i * colort_num + k, pri.isGreen()), yxintiaojian);
                            break;
                        case "斐波":
                            Yongyz.feiBo(getD2list(d2Values, mvs, list, i * colort_num + k, pri.isGreen()), yxintiaojian);
                            break;
                        case "越级":
                            Yongyz.yuJi(getD2list(d2Values, mvs, list, i * colort_num + k, pri.isGreen()), yxintiaojian);
                            break;
                        case "横向124":
                            Yongyz.hengXiang124(getD2list(d2Values, mvs, list, i * colort_num + k, pri.isGreen()), yxintiaojian);
                            break;
                        case "横向137":
                            Yongyz.hengXiang137(getD2list(d2Values, mvs, list, i * colort_num + k, pri.isGreen()), yxintiaojian);
                            break;
                        default:
                            break;
                    }
                    my_d2values[i * colort_num + k] = d2Values;
                }

            }
        }


    }


    public void onClick(View v) {
        if (lock) {
            Toast.makeText(this, "还在计算中...", Toast.LENGTH_SHORT).show();
        } else {
            switch (v.getId()) {
                case R.id.tiaoPlus:
                    handleTiaoPlus();
                    break;
                case R.id.baochun:
                    handleSave();
                    break;
                case R.id.b_keyboard_space:
                    disableRadioGroup(rgMode);
                    if (numXianshi.length() == 2) {
                        char first = numXianshi.charAt(0);
                        char second = numXianshi.charAt(1);
                        int a = getCharInt(second);
                        int b = getCharInt(first);
                        int numInt = 0;
                        if (a >= 10) {
                            numInt = b * 100 + a;
                        } else {
                            numInt = b * 10 + a;
                        }
                        if (b >= a) {
                            Primitive pri;
                            if (currentColor == Colors.BLUE) {
                                pri = new Primitive(numInt, true, isGreen);
                            } else {
                                pri = new Primitive(numInt, false, isGreen);
                            }
                            processPrimitiveSequentially(pri);
                        } else {
                            Toast.makeText(this, "数字不合符规则，添加失败", Toast.LENGTH_SHORT).show();
                        }
                        prepareNewXianShi();
                    } else {
                        Toast.makeText(this, "数字为空或者不合符规则，添加失败", Toast.LENGTH_SHORT).show();
                    }
                    break;
                case R.id.b_keyboard_one:
                    numXianshi = getNumber(numXianshi, "1");
                    boolean eq = numXianshi.equals("11");
                    setTextColor(eq);
                    xianshi_tv.setText(numXianshi);
                    break;
                case R.id.b_keyboard_two:
                    numXianshi = getNumber(numXianshi, "2");
                    eq = numXianshi.equals("22");
                    setTextColor(eq);
                    xianshi_tv.setText(numXianshi);
                    break;
                case R.id.b_keyboard_three:
                    numXianshi = getNumber(numXianshi, "3");
                    eq = numXianshi.equals("33");
                    setTextColor(eq);
                    xianshi_tv.setText(numXianshi);
                    break;
                case R.id.b_keyboard_four:
                    numXianshi = getNumber(numXianshi, "4");
                    eq = numXianshi.equals("44");
                    setTextColor(eq);
                    xianshi_tv.setText(numXianshi);
                    break;
                case R.id.b_keyboard_five:
                    numXianshi = getNumber(numXianshi, "5");
                    eq = numXianshi.equals("55");
                    setTextColor(eq);
                    xianshi_tv.setText(numXianshi);
                    break;
                case R.id.b_keyboard_six:
                    numXianshi = getNumber(numXianshi, "6");
                    eq = numXianshi.equals("66");
                    setTextColor(eq);
                    xianshi_tv.setText(numXianshi);
                    break;
                case R.id.b_keyboard_seven:
                    numXianshi = getNumber(numXianshi, "7");
                    eq = numXianshi.equals("77");
                    setTextColor(eq);
                    xianshi_tv.setText(numXianshi);
                    break;
                case R.id.b_keyboard_eight:
                    numXianshi = getNumber(numXianshi, "8");
                    eq = numXianshi.equals("88");
                    setTextColor(eq);
                    xianshi_tv.setText(numXianshi);
                    break;
                case R.id.b_keyboard_nine:
                    numXianshi = getNumber(numXianshi, "9");
                    eq = numXianshi.equals("99");
                    setTextColor(eq);
                    xianshi_tv.setText(numXianshi);
                    break;
                case R.id.b_keyboard_zero:
                    numXianshi = getNumber(numXianshi, "0");
                    eq = numXianshi.equals("00");
                    setTextColor(eq);
                    xianshi_tv.setText(numXianshi);
                    break;
                case R.id.b_keyboard_t:
                    numXianshi = getNumber(numXianshi, "T");
                    eq = numXianshi.equals("TT");
                    setTextColor(eq);
                    xianshi_tv.setText(numXianshi);
                    break;
                case R.id.b_keyboard_j:
                    numXianshi = getNumber(numXianshi, "J");
                    eq = numXianshi.equals("JJ");
                    setTextColor(eq);
                    xianshi_tv.setText(numXianshi);
                    break;
                case R.id.b_keyboard_q:
                    numXianshi = getNumber(numXianshi, "Q");
                    eq = numXianshi.equals("QQ");
                    setTextColor(eq);
                    xianshi_tv.setText(numXianshi);
                    break;
                case R.id.b_keyboard_k:
                    numXianshi = getNumber(numXianshi, "K");
                    eq = numXianshi.equals("KK");
                    setTextColor(eq);
                    xianshi_tv.setText(numXianshi);
                    break;
                case R.id.b_keyboard_del:
                    if (numXianshi.length() > 0) {
                        numXianshi = numXianshi.substring(0, numXianshi.length() - 1);
                        xianshi_tv.setText(numXianshi);
                    } else {
                        rollback();
                    }
                    rbBlue.setEnabled(true);
                    rbRed.setEnabled(true);
                    rbGreen.setEnabled(false);
                    resetColorRadio();
                    break;
                default:
                    break;
            }
        }
    }

    private void rollback() {
        if (currentS != 1) {
            //定义AlertDialog.Builder对象，当长按列表项的时候弹出确认删除对话框
            AlertDialog.Builder builder = new AlertDialog.Builder(HomeActivity.this);
            builder.setMessage("确定删除?");
            builder.setTitle("提示");

            //添加AlertDialog.Builder对象的setPositiveButton()方法
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Primitive pri = list.remove(list.size() - 1);
                    numS--;
                    currentS--;
                    s_tv.setText("总S: " + numS);
                    currents_tv.setText("" + currentS);
                    hsv_ll1.removeViewAt(numS);
                    hsv_ll1.removeViewAt(numS - 1);
                    hsv_ll2.removeViewAt(numS);
                    prepareNewXianShi();

                    for (int i = 0; i < zubeans.size() * colort_num; ++i) {
                        Zubean zb = zubeans.get(i / colort_num);
                        for (int j = 0; j < zb.getXian(); ++j) {
                            List<MyValue> mv = my_vlist[i][j];
                            if (mv.size() < 3) {
                                mv.clear();
                            } else {
                                mv.remove(mv.size() - 1);
                            }
                        }
                        List<D2Value> d2vs = my_d2values[i];
                        if (!d2vs.isEmpty()) {
                            d2vs.remove(d2vs.size() - 1);
                        }
                        reversemode[i] = false;
                        cuoCounter[i] = 0;
                        for (D2Value d2value : d2vs) {
                            reverseModeCheck(d2value, i);
                        }
                    }

                    if (!d2_sum.isEmpty()) {
                        D2Value d2Value = d2_sum.remove(d2_sum.size() - 1);
                        if ((!pri.isAdengb() || lmode) && (!d2Value.isD2Duicuo() || pri.isGreen())) {
                            cuosum -= Math.abs(Math.round(d2Value.getD2Value()));
                            tv_cuosum.setText(cuosum + "");
                        }
                    }
                    if (!d2_sum.isEmpty()) {
                        long ljz = Math.round(d2_sum.get(d2_sum.size() - 1).getD2ljValue());
                        if (ljz < 0) {
                            tv_sum.setTextColor(HomeActivity.this.getResources().getColor(R.color.red));
                        } else {
                            tv_sum.setTextColor(HomeActivity.this.getResources().getColor(R.color.blue));
                        }
                        tv_sum.setText("" + Math.abs(ljz));
                    }
                    states.remove(states.size() - 1);
                    showState();
                    mainHandler.size = 0;
                    mainHandler.counter = 0;
                    Toast.makeText(HomeActivity.this, "删除成功！", Toast.LENGTH_SHORT).show();
                }
            });

            //添加AlertDialog.Builder对象的setNegativeButton()方法
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });
            builder.create().show();
        }
    }

    private void rollbackBaoCun() {
        if (currentS != 1) {
            //定义AlertDialog.Builder对象，当长按列表项的时候弹出确认删除对话框
            for (int k = currentS; k > 1; k--) {
                Primitive pri = list.remove(k - 2);
                numS--;
                currentS--;
                s_tv.setText("总S: " + numS);
                currents_tv.setText("" + currentS);
                hsv_ll1.removeViewAt(numS);
                hsv_ll1.removeViewAt(numS - 1);
                hsv_ll2.removeViewAt(numS);
                prepareNewXianShi();

                for (int i = 0; i < zubeans.size() * colort_num; ++i) {
                    Zubean zb = zubeans.get(i / colort_num);
                    for (int j = 0; j < zb.getXian(); ++j) {
                        List<MyValue> mv = my_vlist[i][j];
                        if (mv.size() < 3) {
                            mv.clear();
                        } else {
                            mv.remove(mv.size() - 1);
                        }
                    }
                    List<D2Value> d2vs = my_d2values[i];
                    if (!d2vs.isEmpty()) {
                        d2vs.remove(d2vs.size() - 1);
                    }
                    reversemode[i] = false;
                    cuoCounter[i] = 0;
                    for (D2Value d2value : d2vs) {
                        reverseModeCheck(d2value, i);
                    }
                }

                if (!d2_sum.isEmpty()) {
                    D2Value d2Value = d2_sum.remove(d2_sum.size() - 1);
                    if ((!pri.isAdengb() || lmode) && (!d2Value.isD2Duicuo() || pri.isGreen())) {
                        cuosum -= Math.abs(Math.round(d2Value.getD2Value()));
                        tv_cuosum.setText(cuosum + "");
                    }
                }
                if (!d2_sum.isEmpty()) {
                    long ljz = Math.round(d2_sum.get(d2_sum.size() - 1).getD2ljValue());
                    if (ljz < 0) {
                        tv_sum.setTextColor(HomeActivity.this.getResources().getColor(R.color.red));
                    } else {
                        tv_sum.setTextColor(HomeActivity.this.getResources().getColor(R.color.blue));
                    }
                    tv_sum.setText("" + Math.abs(ljz));
                }
                states.clear();
                mainHandler.size = 0;
                mainHandler.counter = 0;
            }
        }


    }

    private void rollbackTiaoJia() {
        if (currentS != 1) {
            //定义AlertDialog.Builder对象，当长按列表项的时候弹出确认删除对话框
            list.clear();
            for (int i = 0; i < my_vlist.length; i++) {
                for (int j = 0; j < my_vlist[i].length; j++) {
                    my_vlist[i][j].clear();
                }
            }

            for (int i = 0; i < my_d2values.length; i++) {
                D2Value d2Value = my_d2values[i].get(my_d2values[i].size() - 1);
                d2Value.setNextValue(my_d2values[i].get(my_d2values[i].size() - 2).getD2Value());
                d2Value.setD2Duicuo(true);
                my_d2values[i].clear();
                my_d2values[i].add(d2Value);
            }
            for (int i = 0; i < d2_sum.size(); i++) {
                D2Value d2Value = d2_sum.get(d2_sum.size() - 1);
                d2Value.setNextValue(d2_sum.get(d2_sum.size() - 2).getD2Value());
                d2Value.setD2Value(0.0);
                d2_sum.clear();
                d2_sum.add(d2Value);
            }
            for (int k = currentS; k > 1; k--) {
                currentS--;
                currents_tv.setText("" + currentS);
                hsv_ll1.removeViewAt(currentS);
                hsv_ll1.removeViewAt(currentS - 1);
                hsv_ll2.removeViewAt(currentS);
                prepareNewXianShi();
                mainHandler.size = 0;
                mainHandler.counter = 0;
            }
            states = null;
            states = new ArrayList<>();
            showState();
            shaomiao_num = 0;
            onShaoMiao(0);
            mainHandler.size = 0;
            mainHandler.counter = 0;
        }
    }

    public String getNumber(String s, String i) {
        String string = "";
        if (s.length() < 2) {
            s = s + i;
            string = s;
            if (string.length() == 2) {
                int first = getCharInt(string.charAt(0));
                int second = getCharInt(string.charAt(1));
                if (first < second) {
                    Toast.makeText(this, "数字过大！", Toast.LENGTH_SHORT).show();
                    string = "";
                }
            }
        } else {
            string = "";
            Toast.makeText(this, "数字过大！", Toast.LENGTH_SHORT).show();
        }
        return string;
    }

    private Yxintiaojian getYxintiaojian() {
        Yxintiaojian yxintiaojian = new Yxintiaojian();
        yxintiaojian.setTongcmax(SPManager.getInstance().getInt("tongcmax", 0));
        yxintiaojian.setTongcmix(SPManager.getInstance().getInt("tongcmix", 0));
        yxintiaojian.setJshu(SPManager.getInstance().getFloat("jshu", 0));
        yxintiaojian.setGongzxian(SPManager.getInstance().getInt("gongzxian", 0));
        yxintiaojian.setFanc(SPManager.getInstance().getFloat("fanc", 0));
        yxintiaojian.setWangfanjd(SPManager.getInstance().getFloat("wangfanjd", 0));
        yxintiaojian.setDzhuangtai(SPManager.getInstance().getString("dzhuangtai", ""));
        yxintiaojian.setYinyongz(SPManager.getInstance().getString("yinyongz", ""));
        yxintiaojian.setZtaixz(SPManager.getInstance().getBoolean("ztaixz", false));
        return yxintiaojian;
    }

    private List<Zubean> getZubeans(Long qun_id, int gongzx) {
        List<Zubean> zubean = new ArrayList<>();
        qunbeanDao = daoSession.getQunbeanDao();
        List<Zubean> zubeanlist = qunbeanDao.load(qun_id).getZu();
        if (yxintiaojian.isZtaixz()) {
            if (zubeanlist.size() >= gongzx) {
                for (int i = 0; i < gongzx; i++) {
                    zubean.add(zubeanlist.get(i));
                }
            }
        } else {
            int j = 0;
            for (int i = 0; i < zubeanlist.size(); i++) {
                if (j < gongzx) {
                    zubean.add(zubeanlist.get(i));
                    j += zubeanlist.get(i).getXian();
                }
            }
        }
        return zubean;
    }

    private List<D2Value> getXD2list(List<D2Value> d2Values, List<MyValue> myVolues, List<Primitive> primitives, final int idx, final boolean green) {
        if (primitives.size() < 4) {
            return null;
        } else if (primitives.size() == 4) {
            if (d2Values.size() < 1) {
                d2Values.get(0).setD2Duicuo(true);
            } else {
                d2Values.get(d2Values.size() - 1).setD2Duicuo(true);

            }
        } else {
            D2Value d2Value = d2Values.get(d2Values.size() - 1);
            Primitive pri = primitives.get(primitives.size() - 1);
            if (pri.isColor()) {
                if (d2Value.isD2mColor()) {
                    d2Value.setD2Duicuo(true);
                } else {
                    d2Value.setD2Duicuo(false);
                }
            } else {
                if (d2Value.isD2mColor()) {
                    d2Value.setD2Duicuo(false);
                } else {
                    d2Value.setD2Duicuo(true);
                }
            }
            d2Value.setD2Lock(pri.isAdengb() && !lmode);
            reverseModeCheck(d2Value, idx);
            if (green) {
                d2Value.setD2Duicuo(false);
            }
            if (d2Value.isD2Duicuo() && !d2Value.isD2Lock()) {
                synchronized (this) {
                    states.get(states.size() - 1).jiaquan_dui++;
                }
            }
        }
        D2Value d2Value1 = new D2Value();
        MyValue mMyValuelist = myVolues.get(primitives.size() - 3);
        if (mMyValuelist.ismColor()) {
            d2Value1.setD2mColor(!reversemode[idx]);
        } else {
            d2Value1.setD2mColor(reversemode[idx]);
        }
        d2Values.add(d2Value1);

        return d2Values;

    }

    /**
     * 负责计算d2的对错
     *
     * @param myVolues
     * @param primitives
     * @return
     */
    private List<D2Value> getD2list(List<D2Value> d2Values, List<MyValue>[] myVolues, List<Primitive> primitives, final int idx, final boolean green) {
        if (primitives.size() < 4) {
            return null;
        } else if (primitives.size() == 4) {
            if (d2Values.size() < 1) {
                d2Values.get(0).setD2Duicuo(true);
            } else {
                d2Values.get(d2Values.size() - 1).setD2Duicuo(true);
            }
        } else {
            D2Value d2Value = d2Values.get(d2Values.size() - 1);
            Primitive pri = primitives.get(primitives.size() - 1);
            if (pri.isColor()) {
                if (d2Value.isD2mColor()) {
                    d2Value.setD2Duicuo(true);
                } else {
                    d2Value.setD2Duicuo(false);
                }
            } else {
                if (d2Value.isD2mColor()) {
                    d2Value.setD2Duicuo(false);
                } else {
                    d2Value.setD2Duicuo(true);
                }
            }
            d2Value.setD2Lock(pri.isAdengb() && !lmode);
            reverseModeCheck(d2Value, idx);
            if (green) {
                d2Value.setD2Duicuo(false);
            }
            if (d2Value.isD2Duicuo() && !d2Value.isD2Lock()) {
                synchronized (this) {
                    states.get(states.size() - 1).jiaquan_dui++;
                }
            }
        }
        D2Value d2Value1 = new D2Value();
        List<MyValue> mMyValuelist = new ArrayList<>();
        for (List<MyValue> myValue : myVolues) {
            mMyValuelist.add(myValue.get(primitives.size() - 3));
        }
        int mColorLockTure = 0;
        int mColorLockFalse = 0;
        for (MyValue myValue1 : mMyValuelist) {
            if (myValue1.ismColor()) {
                mColorLockTure++;
            } else {
                mColorLockFalse++;
            }
        }
        if (mColorLockTure > mColorLockFalse) {
            d2Value1.setD2mColor(!reversemode[idx]);
        } else {
            d2Value1.setD2mColor(reversemode[idx]);
        }
        d2Values.add(d2Value1);

        return d2Values;
    }


    /**
     * 组状态传入二维数据和运行条件，原始数列表，返回二维数据
     */
    private void getD2Values(Lookup lookup, List<Primitive> list, Zubean zubean, List<MyValue> my_d1Values[]) {
        //此方法返回一个组的D2线
        if (list.size() == 4) {
            for (int i = 0; i < zubean.getXian(); i++) {
                List<MyValue> mvs = my_d1Values[i];
                if (zubean.getMos()) {
                    //首查通过默认为对获得下一行的方向和目标色
                    MyValue myValue = new MyValue();
                    myValue.setDuicuo(true);
                    List<Primitive> list_p = new ArrayList<>();
                    list_p.add(list.get(0));
                    list_p.add(list.get(1));
                    list_p.add(list.get(2));
                    try {
                        myValue.setFangx(lookup.firstFangx(FangAnTool.p_Result(list_p,
                                zubean.getFanganhao(), i + list.size() - 1,
                                zubean.getXian()), list.get(3)));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    myValue.setFormula(0);
                    myValue.setmColor(list.get(3).isColor());
                    mvs.add(myValue);
                    MyValue myValue1 = new MyValue();
                    myValue1.setFangx(myValue.isFangx());
                    myValue1.setmColor(lookup.lookupColor(FangAnTool.p_Result(list,
                            zubean.getFanganhao(), i + list.size(),
                            zubean.getXian()), myValue.isFangx()));
                    myValue1.setFormula(0);
                    mvs.add(myValue1);
                } else {
                    //y模式
                    //首查通过默认为对获得下一行的方向和目标色
                    MyValue myValue = new MyValue();
                    myValue.setDuicuo(true);
                    List<Primitive> list_p = new ArrayList<>();
                    list_p.add(list.get(0));
                    list_p.add(list.get(1));
                    list_p.add(list.get(2));
                    myValue.setFangx(lookup.firstFangx(FangAnTool.p_Result(list_p, zubean.getFanganhao(),
                            i + 1, zubean.getXian()), list.get(3)));
                    myValue.setFormula(0);
                    myValue.setmColor(list.get(3).isColor());
                    mvs.add(myValue);
                    MyValue myValue1 = new MyValue();
                    myValue1.setFangx(myValue.isFangx());
                    myValue1.setmColor(lookup.lookupColor(FangAnTool.p_Result(list, zubean.getFanganhao(),
                            i + 1, zubean.getXian()), myValue.isFangx()));
                    myValue1.setFormula(0);
                    mvs.add(myValue1);
                }
            }


        } else if (list.size() > 4) {
            for (int i = 0; i < zubean.getXian(); i++) {
                List<MyValue> mvs = my_d1Values[i];
                if (zubean.getMos()) {
                    List<MyValue> m_list = lookup.lockupVolue(mvs, list, zubean.getFanganhao(), i + list.size(), zubean.getXian());
                    mvs.clear();
                    //此处循环获取多色表的数据
                    my_d1Values[i] = m_list;

                } else {
                    List<MyValue> m_list = lookup.lockupVolue(mvs, list, zubean.getFanganhao(), i + 1, zubean.getXian());
                    mvs.clear();
                    //此处循环获取多色表的数据
                    my_d1Values[i] = m_list;

                }
            }
        }

    }

    /*
     *左侧按钮
     */
    @Override
    protected void onClickLeft() {
        if (list.size() >= 1) {
            Toast.makeText(getApplicationContext(),"请至少保存一组数据！", Toast.LENGTH_LONG).show();

        } else {
            Intent intent = new Intent(HomeActivity.this, ChoiceActivity.class);
            startActivity(intent);
            finish();
        }

    }

    @Override
    protected void onClickRight() {
        super.onClickRight();
        AlertDialog.Builder builder = new AlertDialog.Builder(HomeActivity.this);
        builder.setMessage("确定返回?");
        builder.setTitle("提示");

        //添加AlertDialog.Builder对象的setPositiveButton()方法
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(HomeActivity.this, SetDetailsActivity.class);
                startActivity(intent);
                finish();
            }
        });

        //添加AlertDialog.Builder对象的setNegativeButton()方法
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.create().show();


    }

    public String getLabel() {
        Calendar now = Calendar.getInstance();
        String year = now.get(Calendar.YEAR) + "";
        int m = now.get(Calendar.MONTH) + 1;
        String month = m < 10 ? "0" + m : "" + m;
        int d = now.get(Calendar.DAY_OF_MONTH);
        String day = d < 10 ? "0" + d : "" + d;
        int h = now.get(Calendar.HOUR_OF_DAY);
        String hour = h < 10 ? "0" + h : "" + h;
        int mi = now.get(Calendar.MINUTE);
        String min = mi < 10 ? "0" + mi : "" + mi;
        String mosi = lmode ? "L" : "B";
        String time = year + month + day + hour + min + mosi;
        return time;
    }
}

